package com.authine.cloudpivot.ext.utils;

import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author hxd
 * @Date 2022/4/2 15:36
 * @Description
 **/
@Slf4j
public class CourseCoverWatermarkUtils {

//    public static void main(String[] args) {
//        String sourcePath = "D:/test/zct.png";
//        String targetPath = "D:/test/zct22.png";
//
//        String text = "测试反馈41_xzh";
//        String text1 = "主讲人: 张三";
//        BufferedImage bimage = test(sourcePath, text,text1);
//
//        try {
//
//
//
//            FileOutputStream out = new FileOutputStream(targetPath);
//            ImageIO.write(bimage, "JPEG", out);
//            out.close();
//        } catch (FileNotFoundException e) {
//            log.warn(e.getMessage(), e);
//        } catch (IOException e) {
//            log.warn(e.getMessage(), e);
//        }
//    }



    public static byte[] drawCourseInfoImg(InputStream inputStream,String courseName,String fontColor,double leftRatio,double topRatio,int fontSize ){

        ByteArrayOutputStream baos = null;
        ByteArrayOutputStream bs = null;
        byte[] imageInByte;
        try {
            bs = new ByteArrayOutputStream();
            byte[] imageBytes = new byte[2048];
            int read ;
            while ((read=inputStream.read(imageBytes))!=-1){
                bs.write(imageBytes,0,read);
            }

            ImageIcon imgIcon = new ImageIcon(bs.toByteArray());
            Image img = imgIcon.getImage();
            int width = img.getWidth(null);
            int height = img.getHeight(null);
            BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            Graphics2D g = bimage.createGraphics();
            g.setColor(getColor(fontColor));
            g.setBackground(Color.white);
            g.drawImage(img, 0, 0, null);
            Font font = new Font("SimHei", Font.PLAIN, fontSize);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8F));

            g.setFont(font);


            int left = 0;
            int top = 0;
            // 课程名称水印定位
            left = (int) (width * leftRatio);
            top = (int) (height * topRatio);
            g.drawString(courseName, left, top);


            g.dispose();

            //转byte[]
            baos = new ByteArrayOutputStream();
            ImageIO.write(bimage, "JPEG", baos);
            baos.flush();
            imageInByte = baos.toByteArray();

        } catch (Exception e) {
            log.info(String.format("课程封面水印添加失败:%s", e.getMessage()),e);
            throw new RuntimeException("课程封面水印添加失败");
        }finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (bs != null) {
                try {
                    bs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return imageInByte;
    }


    public static Color getColor(String color) {
        if (color.charAt(0) == '#') {
            color = color.substring(1);
        }
        if (color.length() != 6) {
            return null;
        }
        try {
            int r = Integer.parseInt(color.substring(0, 2), 16);
            int g = Integer.parseInt(color.substring(2, 4), 16);
            int b = Integer.parseInt(color.substring(4), 16);
            return new Color(r, g, b);
        } catch (NumberFormatException nfe) {
            return null;
        }
    }
}
